import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Notificator {

    private String myEmail="nceduprojectnotificator@gmail.com";
    private String password="1QwertY3";
    Session session;

    private static volatile Notificator instance;

    public static synchronized Notificator getInstance() {
        Notificator tempInstance = instance;
        if (tempInstance == null) {
            synchronized (Notificator.class) {
                tempInstance = instance;
                if (tempInstance == null) {
                    instance =tempInstance = new Notificator();
                }
            }
        }
        return tempInstance;
    }

    private Notificator() {

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(myEmail, password);
                    }
                });

    }

    public  synchronized void  sendEmail(String recepientEmail, String subject, String body){

        try {

                Message message = new MimeMessage(session);
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(recepientEmail));
                message.setSubject(subject);
                message.setText(body);

                Transport.send(message);

        } catch (MessagingException e) {
            throw new RuntimeException(e);

        }
    }
}
